<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    function cl(e) {
        if (!e)
            return null;
        var t = e.class
            , n = e.type
            , r = e.column
            , o = e.value;
        return "ENTATTR" == t ?
            "data." + r : "CONST" == t ?
                "STRING" == n ? "'" + o + "'" : "" + o : void 0
    }
    function ul(e, t) {
        var n = e.class
            , r = (e.enabled,
            e.operator)
            , o = e.expressions;
        if ("PDCT" == n)
            return fl(e, t + 1);
        switch (r) {
            case "StartsWith":
                return cl(o[0]) + ".startsWith(" + cl(o[1]) + ")";
            case "NotStartsWith":
                return "!" + cl(o[0]) + ".startsWith(" + cl(o[1]) + ")";
            case "Contains":
                return cl(o[0]) + ".contains(" + cl(o[1]) + ")";
            case "NotContains":
                return "!" + cl(o[0]) + ".contains(" + cl(o[1]) + ")";
            case "Equal":
            case "Field_Equal":
                return cl(o[0]) + "==" + cl(o[1]);
            case "NotEqual":
            case "Field_Not_Equal":
                return cl(o[0]) + "!=" + cl(o[1]);
            case "Less":
            case "Field_Less":
                return cl(o[0]) + "<" + cl(o[1]);
            case "Less_Equal":
            case "Field_Less_Equal":
                return cl(o[0]) + "<=" + cl(o[1]);
            case "Greater":
            case "Field_Greater":
                return cl(o[0]) + ">" + cl(o[1]);
            case "Greater_Equal":
            case "Field_Greater_Equal":
                return cl(o[0]) + ">=" + cl(o[1]);
            case "InList":
                return "lists." + cl(o[1]) + ".containsKey(" + cl(o[0]) + ")";
            case "NotInList":
                return "!lists." + cl(o[1]) + ".containsKey(" + cl(o[0]) + ")";
            case "IsNull":
                return "!" + cl(o[0]);
            case "IsNotNull":
                return cl(o[0])
        }
    }

    // {
    //     linking: "All",
    //         conditions: [{
    //     class: "SMPL",
    //     expressions: [{
    //         column: t.state.searchField,
    //         type: a,
    //         class: "ENTATTR"
    //     }],
    //     enabled: true,
    //     operator: "IsNotNull"
    // }],
    //     class: "PDCT",
    //     enabled: true
    // }

    function fl(e, t) {
        e.class,
            e.enabled;
        var n = e.linking
            , r = e.conditions
            , o = r.length
            , a = "";
        for (var i in "NotAll" != n && "None" != n || (a += "!"),
            a += "(", r)
            if (a += ul(r[i], t), i != o - 1)
                switch (n) {
                    case "All":
                    case "NotAll":
                        a += "&&";
                        break;
                    case "Any":
                    case "None":
                        a += "||"
                }
        return a + ")"
    }
    var dl = function(e, t) {
        if (null == e)
            return "";
        var n = "class " + t + "CheckScript {\n";
        return n += "  public boolean check(def data, def lists) {",
            n += "    if (" + fl(e, 1) + ")\n",
            n += "        return true;\n",
            n += "    else\n",
            n += "        return false;\n",
            n += "}",
            n += "}"
    }
        , hl = function(e) {
        if (null == e)
            return true;
        var t = e.conditions;
        for (var n in t)
            if (!pl(t[n]))
                return false;
        return true
    };
    function pl(e) {
        var t = e.class
            , n = e.operator
            , r = e.expressions;
        if ("PDCT" == t)
            return hl(e);
        if (!n)
            return !1;
        switch (n) {
            case "StartsWith":
            case "NotStartsWith":
            case "Contains":
            case "NotContains":
            case "Equal":
            case "Field_Equal":
            case "NotEqual":
            case "Field_Not_Equal":
            case "Less":
            case "Field_Less":
            case "Less_Equal":
            case "Field_Less_Equal":
            case "Greater":
            case "Field_Greater":
            case "Greater_Equal":
            case "Field_Greater_Equal":
            case "InList":
            case "NotInList":
                return cl(r[1]) && cl(r[0]);
            case "IsNull":
            case "IsNotNull":
                return cl(r[0]) && !0
        }
        return !0
    }

    var ruleScript = dl(ruleDefinition, "Abstraction")

    /*
    label: 指标名
    aggregateType: 聚合条件
    searchField:搜索字段
    searchInterval:时间片
    searchIntervalType：时间片单位 1：年 2：月 3：日 5：日 11：时 12：分 13 秒
    functionField:统计字段
    comment：备注

    fieldList：{
     label,
     value,
     type,
     children:<fieldList>
    }
    searchField: fields.userIP
    */

    handleSubmit = function(e) {
        if (e) {
            var n = {};
            if (0 != t.props.abstraction.id && (n.id = t.props.abstraction.id),
                !t.state.ruleDefinition) {
                for (var r = t.props.fieldList, o = t.state.searchField.split("."), a = "STRING", i = 0; i < r.length; i++)
                    if (r[i].value == o[0]) {
                        for (var s = 0; s < r[i].children.length; s++)
                            if (r[i].children[s].value == o[1]) {
                                if (2 == o.length)
                                    a = r[i].children[s].type;
                                else
                                    for (var l = 0; l < r[i].children[s].children.length; l++)
                                        if (r[i].children[s].children[l].value == o[2]) {
                                            a = r[i].children[s].children[l].type;
                                            break
                                        }
                                break
                            }
                        break
                    }
                t.state.ruleDefinition = {
                    linking: "All",
                    conditions: [{
                        class: "SMPL",
                        expressions: [{
                            column: t.state.searchField,
                            type: a,
                            class: "ENTATTR"
                        }],
                        enabled: true,
                        operator: "IsNotNull"
                    }],
                    class: "PDCT",
                    enabled: true
                }
            }
            if (!hl(t.state.ruleDefinition))
                return ja.error({
                    title: "请检查过滤条件是否配置完整"
                }),
                    !1;
            n.modelId = t.props.modelId,
                n.name = t.state.name,
                n.label = t.state.label,
                n.aggregateType = t.state.aggregateType,
                n.searchField = t.state.searchField,
                n.searchIntervalType = t.state.searchIntervalType,
                n.searchIntervalValue = t.state.searchIntervalValue,
                n.functionField = t.state.functionField,
                n.comment = t.state.comment,
                n.ruleDefinition = t.state.ruleDefinition,
                n.ruleScript = dl(t.state.ruleDefinition, "Abstraction"),
                n.status = 1,
                vl("/abstraction/", "PUT", JSON.stringify(n), function(e) {
                    1 == e.success ? Xa.success("保存成功!") : Xa.error("保存失败！"),
                        t.props.reload()
                })
        } else
            ja.error({
                title: "提交失败",
                content: "请确认表单内容输入正确"
            })
    }
</script>
</body>
</html>
